// pages/view/fs/hospitalIncome/hospitalIncome.js
var wxCharts = require('../../../../utils/wxcharts.js');
const network = require('../../../../utils/network.js')
var app = getApp();
var columnChart = null;
var pieChart = null;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    startTime:'',
    endTime: '',
    chartTitle_1: '科室收入金额',
    chartTitle_2: '科室收入比例',
    cloumn_categories: ['妇产科门诊', '儿保科门诊', '住院妇产科'],
    cloumn_data: [50, 43, 19],


    pie_data: [
      {
        name: '妇产科门诊',
        data: 50
      },
      {
        name: '儿保科门诊',
        data: 43
      },
      {
        name: '住院妇产科',
        data: 19
      }
    ],

    amountList: [],
    totalAmount: ''

  },
  /**
    * 生命周期函数--监听页面加载
    */
  onLoad: function (options) {
    var that = this
    wx.showLoading({
      title: '加载中',
    })
    console.log('options:', options)
    //调用封装好的网络请求，获取数据,
    that.myRequest(options);
    that.setData({
      startTime: options.startTime,
      endTime:options.endTime
    })
  },

  myRequest: function (options){
    var that = this
   network.get('/fs/income?startTime=' + options.startTime + '&endTime=' + options.endTime + '&dept=' + options.dept + '&type=' + options.type, {})
     .then(function (res) {
       console.log('res:', res)
       var obj1 = res.data.data.list1
       var obj2 = res.data.data.list2
       var obj3 = res.data.data.list3
       var obj4 = res.data.data.list4
       var obj5 = res.data.data.list5
       that.setData({

         //科室收入-柱形图
         cloumn_categories: obj1,
         cloumn_data: obj2,
         pie_data: obj4,
         amountList: obj3,
         totalAmount: obj5.totalAmount
       })

       console.log("totalAmount", that.data.totalAmount);
     }).then(function (res) {
      wx.hideLoading()
       that.createColumnChart();
       that.createPieChart();

     }).catch(function (res) {
       console.log(res)
     })
 },
  /**
   * 创建 柱形图
   */
  createColumnChart: function (e) {
    var that = this;
    var windowWidth = 320;
    try {
      var res = wx.getSystemInfoSync();
      windowWidth = res.windowWidth;
    } catch (e) {
      console.error('getSystemInfoSync failed!');
    }

    columnChart = new wxCharts({
      canvasId: 'columnCanvas',
      type: 'column',
      animation: true,
      categories: that.data.cloumn_categories,
      series: [{
        name: '科室',
        data: that.data.cloumn_data,
        format: function (val, name) {

          return val.toFixed(2) + '';
        }
      }],
      yAxis: {
        format: function (val) {
          return val + '（元）';
        },
        title: '金额',
        min: 0
      },
      xAxis: {
        disableGrid: false,
        type: 'calibration'
      },
      extra: {
        column: {
          width: 15
        }
      },
      width: windowWidth,
      height: 200,
    });
  },

  /**
   * 创建 饼状图
   */
  createPieChart: function (e) {
    var that = this;
    var windowWidth = 320;
    try {
      var res = wx.getSystemInfoSync();
      windowWidth = res.windowWidth;
    } catch (e) {
      console.error('getSystemInfoSync failed!');
    }

    pieChart = new wxCharts({
      animation: true,
      canvasId: 'pieCanvas',
      type: 'pie',
      series: that.data.pie_data,
      width: windowWidth,
      height: 300,
      dataLabel: true,
    });
  }


})